/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * humanResourceModule.java
 *
 * Created on Oct 22, 2010, 4:20:08 PM
 */

package restaurantmanagementsystem;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author edwind
 */
public class salesAndMarketingModule extends javax.swing.JFrame {

    /** Creates new form humanResourceModule */
    public salesAndMarketingModule() {
        super("Sales and Marketing");
        initComponents();
        initDatabase();

        this.setSize(850, 630);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = this.getSize();

        int windowX = Math.max(0, (screenSize.width  - windowSize.width ) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);

        this.setLocation(windowX, windowY);
        this.setResizable(false);
       getTableInfo();
        SALESJb.setSelected(true);
        setTable();
        setComboBoxes();
        disableSaveAndUndoButton();
       // employeePanel.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        mainLayeredPane = new javax.swing.JLayeredPane();
        mainPanel = new javax.swing.JPanel();
        toolbarPanel = new javax.swing.JPanel();
        toolbar = new javax.swing.JToolBar();
        buttonNew = new javax.swing.JButton();
        buttonEdit = new javax.swing.JButton();
        buttonSave = new javax.swing.JButton();
        butttonDelete = new javax.swing.JButton();
        toolbar2 = new javax.swing.JToolBar();
        buttonReturn = new javax.swing.JButton();
        buttonLogout = new javax.swing.JButton();
        buttonExit = new javax.swing.JButton();
        topPanel = new javax.swing.JPanel();
        toolbar3 = new javax.swing.JToolBar();
        SALESJb = new javax.swing.JRadioButton();
        INVENTORYJb = new javax.swing.JRadioButton();
        ASSETJb = new javax.swing.JRadioButton();
        toolbar4 = new javax.swing.JToolBar();
        searchButton = new javax.swing.JButton();
        searchField = new javax.swing.JTextField();
        bottomPanel = new javax.swing.JPanel();
        buttonPanel = new javax.swing.JPanel();
        SAVE = new javax.swing.JButton();
        UNDO = new javax.swing.JButton();
        addPanel = new javax.swing.JPanel();
        SalesPanel = new javax.swing.JPanel();
        branchDropDownMenu = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        SalesTotal = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        DAYBox = new javax.swing.JComboBox();
        MONTHBox = new javax.swing.JComboBox();
        YEARField = new javax.swing.JTextField();
        assetPanel = new javax.swing.JPanel();
        labelAssetNo = new javax.swing.JLabel();
        labelAssetName = new javax.swing.JLabel();
        assetNameField = new javax.swing.JTextField();
        assetQTYField = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        assetCostField = new javax.swing.JTextField();
        InventoryPanel = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        VendorBox = new javax.swing.JComboBox();
        InvName = new javax.swing.JTextField();
        InvQty = new javax.swing.JTextField();
        InvCost = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        editPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Table = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(840, 600));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        mainPanel.setBackground(new java.awt.Color(175, 170, 165));
        mainPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        toolbarPanel.setOpaque(false);

        toolbar.setFloatable(false);
        toolbar.setOrientation(javax.swing.SwingConstants.VERTICAL);
        toolbar.setRollover(true);
        toolbar.setOpaque(false);

        buttonNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/new.png"))); // NOI18N
        buttonNew.setText("New");
        buttonNew.setToolTipText("Add a new record");
        buttonNew.setFocusable(false);
        buttonNew.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonNew.setMaximumSize(new java.awt.Dimension(62, 66));
        buttonNew.setMinimumSize(new java.awt.Dimension(62, 66));
        buttonNew.setPreferredSize(new java.awt.Dimension(62, 66));
        buttonNew.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonNew.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buttonNewMouseClicked(evt);
            }
        });
        toolbar.add(buttonNew);

        buttonEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/edit.png"))); // NOI18N
        buttonEdit.setText("Edit");
        buttonEdit.setToolTipText("Edit a record");
        buttonEdit.setFocusable(false);
        buttonEdit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonEdit.setMaximumSize(new java.awt.Dimension(62, 66));
        buttonEdit.setMinimumSize(new java.awt.Dimension(62, 66));
        buttonEdit.setPreferredSize(new java.awt.Dimension(62, 66));
        buttonEdit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buttonEditMouseClicked(evt);
            }
        });
        toolbar.add(buttonEdit);

        buttonSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.png"))); // NOI18N
        buttonSave.setText("Save");
        buttonSave.setToolTipText("Save all changes");
        buttonSave.setFocusable(false);
        buttonSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonSave.setMaximumSize(new java.awt.Dimension(62, 66));
        buttonSave.setMinimumSize(new java.awt.Dimension(62, 66));
        buttonSave.setPreferredSize(new java.awt.Dimension(62, 66));
        buttonSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonSave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buttonSaveMouseClicked(evt);
            }
        });
        buttonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveActionPerformed(evt);
            }
        });
        toolbar.add(buttonSave);

        butttonDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/delete.png"))); // NOI18N
        butttonDelete.setText("Delete");
        butttonDelete.setToolTipText("Delete a record");
        butttonDelete.setFocusable(false);
        butttonDelete.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        butttonDelete.setMaximumSize(new java.awt.Dimension(62, 66));
        butttonDelete.setMinimumSize(new java.awt.Dimension(62, 66));
        butttonDelete.setPreferredSize(new java.awt.Dimension(62, 66));
        butttonDelete.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        butttonDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butttonDeleteMouseClicked(evt);
            }
        });
        toolbar.add(butttonDelete);

        toolbar2.setFloatable(false);
        toolbar2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        toolbar2.setRollover(true);
        toolbar2.setOpaque(false);

        buttonReturn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/home.png"))); // NOI18N
        buttonReturn.setText("Return");
        buttonReturn.setToolTipText("Return to main menu");
        buttonReturn.setFocusable(false);
        buttonReturn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonReturn.setMaximumSize(new java.awt.Dimension(62, 66));
        buttonReturn.setMinimumSize(new java.awt.Dimension(62, 66));
        buttonReturn.setPreferredSize(new java.awt.Dimension(62, 66));
        buttonReturn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolbar2.add(buttonReturn);

        buttonLogout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/logout (1).png"))); // NOI18N
        buttonLogout.setText("Logout");
        buttonLogout.setToolTipText("Log out from your account");
        buttonLogout.setFocusable(false);
        buttonLogout.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonLogout.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolbar2.add(buttonLogout);

        buttonExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit2.png"))); // NOI18N
        buttonExit.setText("Exit");
        buttonExit.setToolTipText("Close the program");
        buttonExit.setFocusable(false);
        buttonExit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonExit.setMaximumSize(new java.awt.Dimension(62, 66));
        buttonExit.setMinimumSize(new java.awt.Dimension(62, 66));
        buttonExit.setPreferredSize(new java.awt.Dimension(62, 66));
        buttonExit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonExitActionPerformed(evt);
            }
        });
        toolbar2.add(buttonExit);

        javax.swing.GroupLayout toolbarPanelLayout = new javax.swing.GroupLayout(toolbarPanel);
        toolbarPanel.setLayout(toolbarPanelLayout);
        toolbarPanelLayout.setHorizontalGroup(
            toolbarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(toolbarPanelLayout.createSequentialGroup()
                .addGroup(toolbarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(toolbar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(toolbar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        toolbarPanelLayout.setVerticalGroup(
            toolbarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, toolbarPanelLayout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(toolbar, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(toolbar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(158, 158, 158))
        );

        mainPanel.add(toolbarPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 70, 600));

        topPanel.setOpaque(false);
        topPanel.setPreferredSize(new java.awt.Dimension(790, 490));

        toolbar3.setBorder(null);
        toolbar3.setFloatable(false);
        toolbar3.setRollover(true);
        toolbar3.setOpaque(false);
        toolbar3.setVerifyInputWhenFocusTarget(false);

        buttonGroup1.add(SALESJb);
        SALESJb.setText("Sales");
        SALESJb.setContentAreaFilled(false);
        SALESJb.setFocusable(false);
        SALESJb.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        SALESJb.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SALESJbMouseClicked(evt);
            }
        });
        toolbar3.add(SALESJb);

        buttonGroup1.add(INVENTORYJb);
        INVENTORYJb.setText("Inventory");
        INVENTORYJb.setContentAreaFilled(false);
        INVENTORYJb.setFocusable(false);
        INVENTORYJb.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        INVENTORYJb.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        INVENTORYJb.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                INVENTORYJbMouseClicked(evt);
            }
        });
        toolbar3.add(INVENTORYJb);

        buttonGroup1.add(ASSETJb);
        ASSETJb.setText("Asset");
        ASSETJb.setContentAreaFilled(false);
        ASSETJb.setFocusable(false);
        ASSETJb.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        ASSETJb.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        ASSETJb.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ASSETJbMouseClicked(evt);
            }
        });
        toolbar3.add(ASSETJb);

        toolbar4.setBorder(null);
        toolbar4.setFloatable(false);
        toolbar4.setRollover(true);
        toolbar4.setOpaque(false);

        searchButton.setText("Search");
        searchButton.setFocusable(false);
        searchButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        searchButton.setMaximumSize(new java.awt.Dimension(70, 20));
        searchButton.setMinimumSize(new java.awt.Dimension(70, 20));
        searchButton.setPreferredSize(new java.awt.Dimension(70, 20));
        searchButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });
        toolbar4.add(searchButton);

        searchField.setFont(new java.awt.Font("Ubuntu", 2, 12));
        searchField.setText("Enter Keyword");
        searchField.setMaximumSize(new java.awt.Dimension(200, 20));
        searchField.setMinimumSize(new java.awt.Dimension(200, 20));
        searchField.setPreferredSize(new java.awt.Dimension(200, 20));
        searchField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                searchFieldKeyReleased(evt);
            }
        });
        toolbar4.add(searchField);

        javax.swing.GroupLayout topPanelLayout = new javax.swing.GroupLayout(topPanel);
        topPanel.setLayout(topPanelLayout);
        topPanelLayout.setHorizontalGroup(
            topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(topPanelLayout.createSequentialGroup()
                .addComponent(toolbar3, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(507, Short.MAX_VALUE))
            .addGroup(topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(topPanelLayout.createSequentialGroup()
                    .addContainerGap(528, Short.MAX_VALUE)
                    .addComponent(toolbar4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        topPanelLayout.setVerticalGroup(
            topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(toolbar3, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
            .addGroup(topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(toolbar4, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE))
        );

        mainPanel.add(topPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 0, 800, 40));

        bottomPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        SAVE.setText("Save");
        SAVE.setEnabled(false);
        SAVE.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SAVEMouseClicked(evt);
            }
        });

        UNDO.setText("Undo");
        UNDO.setEnabled(false);
        UNDO.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                UNDOMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SAVE, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                    .addComponent(UNDO, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE))
                .addContainerGap())
        );
        buttonPanelLayout.setVerticalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(SAVE)
                .addGap(26, 26, 26)
                .addComponent(UNDO)
                .addContainerGap(46, Short.MAX_VALUE))
        );

        bottomPanel.add(buttonPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(640, 0, -1, -1));

        addPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("New"));
        addPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        SalesPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        branchDropDownMenu.setFont(new java.awt.Font("Ubuntu", 0, 12));
        branchDropDownMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                branchDropDownMenuActionPerformed(evt);
            }
        });
        SalesPanel.add(branchDropDownMenu, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 10, 160, 20));

        jLabel2.setFont(new java.awt.Font("Ubuntu", 0, 12));
        jLabel2.setText("Sales Total    :");
        SalesPanel.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 130, 20));

        jLabel1.setFont(new java.awt.Font("Ubuntu", 0, 12));
        jLabel1.setText("Branch Name:");
        SalesPanel.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 120, 20));

        SalesTotal.setFont(new java.awt.Font("Ubuntu", 0, 12));
        SalesTotal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                SalesTotalKeyTyped(evt);
            }
        });
        SalesPanel.add(SalesTotal, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 60, 160, 20));

        jLabel7.setFont(new java.awt.Font("Ubuntu", 0, 12));
        jLabel7.setText("Sales Date :");
        SalesPanel.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 10, -1, 30));

        DAYBox.setFont(new java.awt.Font("Ubuntu", 0, 12));
        DAYBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        SalesPanel.add(DAYBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 50, -1, 20));

        MONTHBox.setFont(new java.awt.Font("Ubuntu", 0, 12));
        MONTHBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }));
        SalesPanel.add(MONTHBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 50, -1, 20));

        YEARField.setFont(new java.awt.Font("Ubuntu", 0, 12));
        YEARField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                YEARFieldActionPerformed(evt);
            }
        });
        YEARField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                YEARFieldKeyTyped(evt);
            }
        });
        SalesPanel.add(YEARField, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 50, 80, -1));

        addPanel.add(SalesPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(18, 21, 544, 111));

        assetPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        labelAssetNo.setFont(new java.awt.Font("Ubuntu", 0, 12));
        labelAssetNo.setText("Asset Name   : ");
        assetPanel.add(labelAssetNo, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));

        labelAssetName.setFont(new java.awt.Font("Ubuntu", 0, 12));
        labelAssetName.setText("Asset Quantity: ");
        assetPanel.add(labelAssetName, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 50, -1, -1));

        assetNameField.setFont(new java.awt.Font("Ubuntu", 0, 12));
        assetNameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                assetNameFieldKeyTyped(evt);
            }
        });
        assetPanel.add(assetNameField, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 20, 129, 20));

        assetQTYField.setFont(new java.awt.Font("Ubuntu", 0, 12));
        assetQTYField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                assetQTYFieldKeyTyped(evt);
            }
        });
        assetPanel.add(assetQTYField, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 50, 129, 20));

        jLabel8.setFont(new java.awt.Font("Ubuntu", 0, 12));
        jLabel8.setText("Asset Cost :");
        assetPanel.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 20, -1, 20));

        assetCostField.setFont(new java.awt.Font("Ubuntu", 0, 12));
        assetCostField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                assetCostFieldKeyTyped(evt);
            }
        });
        assetPanel.add(assetCostField, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 20, 130, 20));

        addPanel.add(assetPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(29, 33, 533, 99));

        InventoryPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setFont(new java.awt.Font("Ubuntu", 0, 12)); // NOI18N
        jLabel3.setText("Inventory Name:");
        InventoryPanel.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, 20));

        jLabel4.setFont(new java.awt.Font("Ubuntu", 0, 12)); // NOI18N
        jLabel4.setText("Inventory Quantity:");
        InventoryPanel.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 60, -1, 20));

        jLabel5.setFont(new java.awt.Font("Ubuntu", 0, 12)); // NOI18N
        jLabel5.setText("Inventory Cost:");
        InventoryPanel.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 10, -1, 20));

        VendorBox.setFont(new java.awt.Font("Ubuntu", 0, 12)); // NOI18N
        VendorBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VendorBoxActionPerformed(evt);
            }
        });
        InventoryPanel.add(VendorBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 50, 140, 20));

        InvName.setFont(new java.awt.Font("Ubuntu", 0, 12)); // NOI18N
        InvName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InvNameKeyTyped(evt);
            }
        });
        InventoryPanel.add(InvName, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 10, 130, 20));

        InvQty.setFont(new java.awt.Font("Ubuntu", 0, 12)); // NOI18N
        InvQty.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InvQtyKeyTyped(evt);
            }
        });
        InventoryPanel.add(InvQty, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 50, 130, 20));

        InvCost.setFont(new java.awt.Font("Ubuntu", 0, 12)); // NOI18N
        InvCost.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InvCostKeyTyped(evt);
            }
        });
        InventoryPanel.add(InvCost, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 10, 140, 20));

        jLabel6.setFont(new java.awt.Font("Ubuntu", 0, 12)); // NOI18N
        jLabel6.setText("Vendor Name:");
        InventoryPanel.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 50, -1, 20));

        addPanel.add(InventoryPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(15, 32, 550, 100));

        bottomPanel.add(addPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, 580, 150));

        editPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Edit"));

        javax.swing.GroupLayout editPanelLayout = new javax.swing.GroupLayout(editPanel);
        editPanel.setLayout(editPanelLayout);
        editPanelLayout.setHorizontalGroup(
            editPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 568, Short.MAX_VALUE)
        );
        editPanelLayout.setVerticalGroup(
            editPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 123, Short.MAX_VALUE)
        );

        bottomPanel.add(editPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, 580, 150));

        mainPanel.add(bottomPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 410, 800, -1));

        Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(Table);

        mainPanel.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 40, 800, 370));

        mainPanel.setBounds(0, 0, 870, 600);
        mainLayeredPane.add(mainPanel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        getContentPane().add(mainLayeredPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 870, 600));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SALESJbMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SALESJbMouseClicked
        setTable();
}//GEN-LAST:event_SALESJbMouseClicked

    private void INVENTORYJbMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_INVENTORYJbMouseClicked
        setTable();
}//GEN-LAST:event_INVENTORYJbMouseClicked

    private void ASSETJbMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ASSETJbMouseClicked
        setTable();
}//GEN-LAST:event_ASSETJbMouseClicked

    private void butttonDeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butttonDeleteMouseClicked
        // TODO add your handling code here:
        confirmationDelete();
    }//GEN-LAST:event_butttonDeleteMouseClicked

    private void buttonSaveMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonSaveMouseClicked
        // TODO add your handling code here:
        confirmationMessage();
    }//GEN-LAST:event_buttonSaveMouseClicked

    private void branchDropDownMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_branchDropDownMenuActionPerformed
        // TODO add your handling code here:
            name = (String)branchDropDownMenu.getSelectedItem();
                try {

            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT * FROM BRANCH WHERE BRANCH_NAME = '" + name + "'");

            while(rset.next()) {
                code = rset.getString(1);
            }

            stmt.close();

        } catch (Exception e) {
            System.out.println("ERROR => " + e.getMessage());
        }
    }//GEN-LAST:event_branchDropDownMenuActionPerformed

    private void buttonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_buttonSaveActionPerformed

    private void buttonExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonExitActionPerformed
        // TODO add your handling code here:
        promptSaveBeforeExit();
    }//GEN-LAST:event_buttonExitActionPerformed

    private void buttonNewMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonNewMouseClicked
        // TODO add your handling code here:
        selectMode = 0;
        clearFields();
    }//GEN-LAST:event_buttonNewMouseClicked

    private void buttonEditMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonEditMouseClicked
        // TODO add your handling code here:
        selectMode = 1;
    }//GEN-LAST:event_buttonEditMouseClicked

    private void VendorBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_VendorBoxActionPerformed
        // TODO add your handling code here:
               name = (String)branchDropDownMenu.getSelectedItem();
                try {

            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT * FROM VENDOR WHERE VENDOR_NAME = '" + name + "'");

            while(rset.next()) {
                code = rset.getString(1);
            }

            stmt.close();

        } catch (Exception e) {
            System.out.println("ERROR => " + e.getMessage());
        }
    }//GEN-LAST:event_VendorBoxActionPerformed

    private void InvCostKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_InvCostKeyTyped
        // TODO add your handling code here:
        enableSaveAndUndoButton();
    }//GEN-LAST:event_InvCostKeyTyped

    private void InvQtyKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_InvQtyKeyTyped
        // TODO add your handling code here:
        enableSaveAndUndoButton();
    }//GEN-LAST:event_InvQtyKeyTyped

    private void InvNameKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_InvNameKeyTyped
        // TODO add your handling code here:
        enableSaveAndUndoButton();
    }//GEN-LAST:event_InvNameKeyTyped

    private void SalesTotalKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_SalesTotalKeyTyped
        // TODO add your handling code here:
        enableSaveAndUndoButton();
    }//GEN-LAST:event_SalesTotalKeyTyped

    private void SAVEMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SAVEMouseClicked
        // TODO add your handling code here:
        confirmationMessage();
    }//GEN-LAST:event_SAVEMouseClicked

    private void UNDOMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_UNDOMouseClicked
        // TODO add your handling code here:
        clearFields();
    }//GEN-LAST:event_UNDOMouseClicked

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        // TODO add your handling code here:
        Dimension THIS_SIZE = (mainLayeredPane.getSize());

        Table.setMaximumSize(THIS_SIZE);
        Table.setMinimumSize(THIS_SIZE);
        Table.setPreferredSize(THIS_SIZE);

        countTable1Row = 0;
        countTable1Column = 0;
        countTable2Row = 0;
        countTable2Column = 0;
        countTable3Row = 0;
        countTable3Column = 0;
        rowIncrement = 0;
        columnIncrement = 0;

        int n = selectedButton();
        if (n == 0){
        try{
            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT* FROM SALES WHERE (Month(SALES_DATE) = Month(" + searchField.getText() + ")");

            //creating 2d arrays of appropriate size according to database
            while (rset.next()){
                countTable1Row++;
            }

            ResultSetMetaData metadata = rset.getMetaData();
            countTable1Column = metadata.getColumnCount();

            newTableSales.setDataSize(countTable1Row, countTable1Column);
            newTableSales.setColumnNameSize();
            //end

            //adding column names to table model
            newTableSales.setColumnName(0, "Sales No.");
            newTableSales.setColumnName(1, "Total Sales");
            newTableSales.setColumnName(2, "Sales Date");
            newTableSales.setColumnName(3, "Branch No.");
            // end

            rset = stmt.executeQuery("SELECT* FROM SALES WHERE (Month(SALES_DATE) = Month(" + searchField.getText() + ") ORDER BY SALES_NO");
            while(rset.next()){
                newTableSales.addString(rowIncrement, columnIncrement, rset.getString("SALES_NO"));
                columnIncrement++;
                newTableSales.addString(rowIncrement, columnIncrement, rset.getString("SALES_TOTAL"));
                columnIncrement++;
                newTableSales.addString(rowIncrement, columnIncrement, rset.getString("SALES_DATE").substring(0,10));
                columnIncrement++;
                newTableSales.addString(rowIncrement, columnIncrement, rset.getString("BRANCH_NO"));
                columnIncrement = 0;
                rowIncrement++;
            }
            }
            catch(Exception e){
            System.out.println("Error connecting database");
            }
        }
        else if(n == 1){
            try{
            newTableInventory = new NewTableModel();
            rowIncrement = 0;
            columnIncrement = 0;

            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT* FROM INVENTORY WHERE (INV_NAME LIKE '" + searchField.getText() + "%' OR INV_NO LIKE '" + searchField.getText() + "%')");

            //creating 2d arrays of appropriate size according to database
            while (rset.next()){
                countTable2Row++;
            }

            ResultSetMetaData metadata = rset.getMetaData();
            countTable2Column = metadata.getColumnCount();

            newTableInventory.setDataSize(countTable2Row, countTable2Column);
            newTableInventory.setColumnNameSize();
            //end

            //adding column names to table model
            newTableInventory.setColumnName(0, "Inventory No.");
            newTableInventory.setColumnName(1, "Inventory Name");
            newTableInventory.setColumnName(2, "Inventory Quantity");
            newTableInventory.setColumnName(3, "Inventory Cost");
            newTableInventory.setColumnName(4, "Vendor No.");
            // end

            rset = stmt.executeQuery("SELECT * FROM INVENTORY WHERE (INV_NAME LIKE '" + searchField.getText() + "%' OR INV_NO LIKE '" + searchField.getText() + "%') ORDER BY INV_NO");
            while(rset.next()){
                newTableInventory.addString(rowIncrement, columnIncrement, rset.getString("INV_NO"));
                columnIncrement++;
                newTableInventory.addString(rowIncrement, columnIncrement, rset.getString("INV_NAME"));
                columnIncrement++;
                newTableInventory.addString(rowIncrement, columnIncrement, rset.getString("INV_QTY"));
                columnIncrement++;
                newTableInventory.addString(rowIncrement, columnIncrement, rset.getString("INV_COST"));
                columnIncrement++;
                newTableInventory.addString(rowIncrement, columnIncrement, rset.getString("VENDOR_NO"));
                columnIncrement = 0;
                rowIncrement++;
            }
            }
            catch(Exception e){
            System.out.println("Error connecting database");
                            }
        }
     else if (n == 2){
            try{
            newTableAsset = new NewTableModel();
            rowIncrement = 0;
            columnIncrement = 0;

            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT* FROM ASSET WHERE ASSET_NAME LIKE '" + searchField.getText() + "%'");

            //creating 2d arrays of appropriate size according to database
            while (rset.next()){
                countTable3Row++;
            }

            ResultSetMetaData metadata = rset.getMetaData();
            countTable3Column = metadata.getColumnCount();

            newTableAsset.setDataSize(countTable3Row, countTable3Column);
            newTableAsset.setColumnNameSize();
            //end

            //adding column names to table model
            newTableAsset.setColumnName(0, "Asset No.");
            newTableAsset.setColumnName(1, "Asset Name");
             newTableAsset.setColumnName(2, "Asset Quantity");
            newTableAsset.setColumnName(3, "Asset Cost");
            // end

            rset = stmt.executeQuery("SELECT* FROM ASSET WHERE ASSET_NAME LIKE '" + searchField.getText() + "%'");
            while(rset.next()){
                newTableAsset.addString(rowIncrement, columnIncrement, rset.getString("ASSET_NO"));
                columnIncrement++;
                newTableAsset.addString(rowIncrement, columnIncrement, rset.getString("ASSET_NAME"));
                columnIncrement++;
                newTableAsset.addString(rowIncrement, columnIncrement, rset.getString("ASSET_QTY"));
                columnIncrement++;
                newTableAsset.addString(rowIncrement, columnIncrement, rset.getString("ASSET_COST"));
                columnIncrement = 0;
                rowIncrement++;
            }
        }
        catch(Exception e){
            System.out.println("Error connecting database");
        }
        }
        setTable();
    }//GEN-LAST:event_searchButtonActionPerformed

    private void searchFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_searchFieldKeyReleased
        // TODO add your handling code here:
        getTableInfo();
        setTable();
    }//GEN-LAST:event_searchFieldKeyReleased

    private void assetNameFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_assetNameFieldKeyTyped
        enableSaveAndUndoButton();
        // TODO add your handling code here:
}//GEN-LAST:event_assetNameFieldKeyTyped

    private void assetQTYFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_assetQTYFieldKeyTyped
        enableSaveAndUndoButton();
        // TODO add your handling code here:
}//GEN-LAST:event_assetQTYFieldKeyTyped

    private void YEARFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_YEARFieldActionPerformed
        // TODO add your handling code here:

    }//GEN-LAST:event_YEARFieldActionPerformed

    private void YEARFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_YEARFieldKeyTyped
        // TODO add your handling code here:
        enableSaveAndUndoButton();
    }//GEN-LAST:event_YEARFieldKeyTyped

    private void assetCostFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_assetCostFieldKeyTyped
        // TODO add your handling code here:
        enableSaveAndUndoButton();
    }//GEN-LAST:event_assetCostFieldKeyTyped

    private void ASSETJbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ASSETJbActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ASSETJbActionPerformed

    private void searchFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_searchFieldActionPerformed

    private void SalesTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SalesTotalActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_SalesTotalActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new salesAndMarketingModule().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton ASSETJb;
    private javax.swing.JComboBox DAYBox;
    private javax.swing.JRadioButton INVENTORYJb;
    private javax.swing.JTextField InvCost;
    private javax.swing.JTextField InvName;
    private javax.swing.JTextField InvQty;
    private javax.swing.JPanel InventoryPanel;
    private javax.swing.JComboBox MONTHBox;
    private javax.swing.JRadioButton SALESJb;
    private javax.swing.JButton SAVE;
    private javax.swing.JPanel SalesPanel;
    private javax.swing.JTextField SalesTotal;
    private javax.swing.JTable Table;
    private javax.swing.JButton UNDO;
    private javax.swing.JComboBox VendorBox;
    private javax.swing.JTextField YEARField;
    private javax.swing.JPanel addPanel;
    private javax.swing.JTextField assetCostField;
    private javax.swing.JTextField assetNameField;
    private javax.swing.JPanel assetPanel;
    private javax.swing.JTextField assetQTYField;
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JComboBox branchDropDownMenu;
    private javax.swing.JButton buttonEdit;
    private javax.swing.JButton buttonExit;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton buttonLogout;
    private javax.swing.JButton buttonNew;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton buttonReturn;
    private javax.swing.JButton buttonSave;
    private javax.swing.JButton butttonDelete;
    private javax.swing.JPanel editPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelAssetName;
    private javax.swing.JLabel labelAssetNo;
    private javax.swing.JLayeredPane mainLayeredPane;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField searchField;
    private javax.swing.JToolBar toolbar;
    private javax.swing.JToolBar toolbar2;
    private javax.swing.JToolBar toolbar3;
    private javax.swing.JToolBar toolbar4;
    private javax.swing.JPanel toolbarPanel;
    private javax.swing.JPanel topPanel;
    // End of variables declaration//GEN-END:variables

    Connection con;
    NewTableModel newTableSales = new NewTableModel();
    NewTableModel newTableInventory;
    NewTableModel newTableAsset;
    int countTable1Row = 0;
    int countTable1Column = 0;
    int countTable2Row = 0;
    int countTable2Column = 0;
    int countTable3Row = 0;
    int countTable3Column = 0;
    int rowIncrement = 0;
    int columnIncrement = 0;
    String code, name;
    String[] vendorMenuItem;
    String[] branchMenuItem;
    int selectMode = 0;

    public void initDatabase(){
        try{
            DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
            con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "EDWIND", "cat200");
            System.out.println("Database Connected");
        }
        catch(Exception e){
            System.out.println("Cannot connect to database");
        }
    }

    public void getTableInfo(){


        Dimension THIS_SIZE = (mainLayeredPane.getSize());

        Table.setMaximumSize(THIS_SIZE);
        Table.setMinimumSize(THIS_SIZE);
        Table.setPreferredSize(THIS_SIZE);

        countTable1Row = 0;
        countTable1Column = 0;
        countTable2Row = 0;
        countTable2Column = 0;
        countTable3Row = 0;
        countTable3Column = 0;
        rowIncrement = 0;
        columnIncrement = 0;


        try{
            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT* FROM SALES");

            //creating 2d arrays of appropriate size according to database
            while (rset.next()){
                countTable1Row++;
            }

            ResultSetMetaData metadata = rset.getMetaData();
            countTable1Column = metadata.getColumnCount();

            newTableSales.setDataSize(countTable1Row, countTable1Column);
            newTableSales.setColumnNameSize();
            //end

            //adding column names to table model
            newTableSales.setColumnName(0, "Sales No.");
            newTableSales.setColumnName(1, "Total Sales");
            newTableSales.setColumnName(2, "Sales Date");
            newTableSales.setColumnName(3, "Branch No.");
            // end

            rset = stmt.executeQuery("SELECT* FROM SALES");
            while(rset.next()){
                newTableSales.addString(rowIncrement, columnIncrement, rset.getString("SALES_NO"));
                columnIncrement++;
                newTableSales.addString(rowIncrement, columnIncrement, rset.getString("SALES_TOTAL"));
                columnIncrement++;
                newTableSales.addString(rowIncrement, columnIncrement, rset.getString("SALES_DATE").substring(0, 10));
                columnIncrement++;
                newTableSales.addString(rowIncrement, columnIncrement, rset.getString("BRANCH_NO"));
                columnIncrement = 0;
                rowIncrement++;
            }

            newTableInventory = new NewTableModel();
            rowIncrement = 0;
            columnIncrement = 0;

            rset = stmt.executeQuery("SELECT* FROM INVENTORY");

            //creating 2d arrays of appropriate size according to database
            while (rset.next()){
                countTable2Row++;
            }

            metadata = rset.getMetaData();
            countTable2Column = metadata.getColumnCount();

            newTableInventory.setDataSize(countTable2Row, countTable2Column);
            newTableInventory.setColumnNameSize();
            //end

            //adding column names to table model
            newTableInventory.setColumnName(0, "Inventory No.");
            newTableInventory.setColumnName(1, "Inventory Name");
            newTableInventory.setColumnName(2, "Inventory Quantity");
            newTableInventory.setColumnName(3, "Inventory Cost");
            newTableInventory.setColumnName(4, "Vendor No.");
            // end

            rset = stmt.executeQuery("SELECT* FROM INVENTORY");
            while(rset.next()){
                newTableInventory.addString(rowIncrement, columnIncrement, rset.getString("INV_NO"));
                columnIncrement++;
                newTableInventory.addString(rowIncrement, columnIncrement, rset.getString("INV_NAME"));
                columnIncrement++;
                newTableInventory.addString(rowIncrement, columnIncrement, rset.getString("INV_QTY"));
                columnIncrement++;
                newTableInventory.addString(rowIncrement, columnIncrement, rset.getString("INV_COST"));
                columnIncrement++;
                newTableInventory.addString(rowIncrement, columnIncrement, rset.getString("VENDOR_NO"));
                columnIncrement = 0;
                rowIncrement++;
            }

            newTableAsset = new NewTableModel();
            rowIncrement = 0;
            columnIncrement = 0;

            rset = stmt.executeQuery("SELECT* FROM ASSET");

            //creating 2d arrays of appropriate size according to database
            while (rset.next()){
                countTable3Row++;
            }

            metadata = rset.getMetaData();
            countTable3Column = metadata.getColumnCount();

            newTableAsset.setDataSize(countTable3Row, countTable3Column);
            newTableAsset.setColumnNameSize();
            //end

            //adding column names to table model
            newTableAsset.setColumnName(0, "Asset No.");
            newTableAsset.setColumnName(1, "Asset Name");
             newTableAsset.setColumnName(2, "Asset Quantity");
            newTableAsset.setColumnName(3, "Asset Cost");
            // end

            rset = stmt.executeQuery("SELECT* FROM ASSET");
            while(rset.next()){
                newTableAsset.addString(rowIncrement, columnIncrement, rset.getString("ASSET_NO"));
                columnIncrement++;
                newTableAsset.addString(rowIncrement, columnIncrement, rset.getString("ASSET_NAME"));
                columnIncrement++;
                newTableAsset.addString(rowIncrement, columnIncrement, rset.getString("ASSET_QTY"));
                columnIncrement++;
                newTableAsset.addString(rowIncrement, columnIncrement, rset.getString("ASSET_COST"));
                columnIncrement = 0;
                rowIncrement++;
            }
        }
        catch(Exception e){
            System.out.println("Error connecting database");
        }
    }

    public void setTable(){
        
        Table.setModel(selectedTable());
        Table.show();

        ListSelectionModel selectionModel = Table.getSelectionModel();

            selectionModel.addListSelectionListener(new ListSelectionListener(){

            public void valueChanged(ListSelectionEvent e) {
                if (Table.getSelectedRow() != -1){
                    //panel.setBorder(javax.swing.BorderFactory.createTitledBorder("Edit"));
                    selectMode = 1;
                    getRecordInfo();
                    disableSaveAndUndoButton();
                    addAndEdit();
                }
            }
        }
        );

    }

    public NewTableModel selectedTable(){

        int n = selectedButton();
        if (n == 0){
            return newTableSales;
        }
        else if (n == 1){
            return newTableInventory;
        }

        return newTableAsset;
    }

   public int selectedButton(){

        if (SALESJb.isSelected()){
            System.out.print("Sales Table Selected\n");
            SalesPanel.setVisible(true);
            InventoryPanel.setVisible(false);
            assetPanel.setVisible(false);
            return 0;
        }
        else if (INVENTORYJb.isSelected()){
            System.out.print("Inventory Table Selected\n");
            SalesPanel.setVisible(false);
            InventoryPanel.setVisible(true);
            assetPanel.setVisible(false);
            return 1;
        }


        System.out.print("Asset Table Selected\n");
        SalesPanel.setVisible(false);
        InventoryPanel.setVisible(false);
        assetPanel.setVisible(true);
        return 2;
    }

    public void getRecordInfo(){

        int i = 0;
        int n = selectedButton();

        if (n == 0)
        {
        String num = (String)newTableSales.getValueAt(Table.getSelectedRow(), 2);
        Object name = "";

        try {

            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT BRANCH_NAME FROM BRANCH WHERE BRANCH_NO = '" + num + "'");

            while(rset.next()) {
                name = rset.getObject("BRANCH_NAME");
            }

            stmt.close();

        } catch (Exception e) {
            System.out.println("ERROR => " + e.getMessage());
        }


        SalesTotal.setText((String) newTableSales.getValueAt(Table.getSelectedRow(), 1));
        branchDropDownMenu.setSelectedItem(name);
        }
    else if(n == 1)
        {
        String num = (String)newTableInventory.getValueAt(Table.getSelectedRow(), 4);
        Object name = "";

        try {

            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT VENDOR_NAME FROM VENDOR WHERE VENDOR_NO = '" + num + "'");

            while(rset.next()) {
                name = rset.getObject("VENDOR_NAME");
            }

            stmt.close();

        } catch (Exception e) {
            System.out.println("ERROR => " + e.getMessage());
        }


        InvName.setText((String) newTableInventory.getValueAt(Table.getSelectedRow(), 1));
        InvQty.setText((String) newTableInventory.getValueAt(Table.getSelectedRow(), 2));
        InvCost.setText((String) newTableInventory.getValueAt(Table.getSelectedRow(), 3));
        VendorBox.setSelectedItem(name);
    }
    }

    public void clearFields(){
        InvName.setText("");
        InvQty.setText("");
        InvCost.setText("");
        SalesTotal.setText("");
        YEARField.setText("");
        assetNameField.setText("");
        assetQTYField.setText("");
        assetCostField.setText("");
        VendorBox.setSelectedIndex(0);
        branchDropDownMenu.setSelectedIndex(0);
    }

    public void addAndEdit(){
        int n = selectedButton();
        if (n==0)
        {
        SalesPanel.setVisible(true);
        InventoryPanel.setVisible(false);
        }
        else if (n==1)
        {
        SalesPanel.setVisible(false);
        InventoryPanel.setVisible(true);
        }

    }

    public void setComboBoxes(){
        setVendorDropDownMenu();
        setBranchDropDownMenu();
    }
    
    public void setVendorDropDownMenu(){

        int countJobTableRow = 0;
        int i = 0;

        try{
            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT VENDOR_NAME FROM VENDOR");

            while (rset.next()){
                countJobTableRow++;
            }

            vendorMenuItem = new String[countJobTableRow];

            rset = stmt.executeQuery("SELECT VENDOR_NAME FROM VENDOR ORDER BY VENDOR_NO");

            while (rset.next()){
                vendorMenuItem[i] = rset.getString("VENDOR_NAME");
                i++;
            }

            i = 0;

            for (i = 0; i < countJobTableRow; i++){
                VendorBox.addItem(vendorMenuItem[i]);
            }

        }
        catch (Exception e){
            System.out.println("Error connecting database");
        }
    }
    
    public void setBranchDropDownMenu(){

        int countBranchTableRow = 0;
        int i = 0;

        try{
            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT BRANCH_NAME FROM BRANCH");

            while (rset.next()){
                countBranchTableRow++;
            }

            branchMenuItem = new String[countBranchTableRow];

            rset = stmt.executeQuery("SELECT BRANCH_NAME FROM BRANCH ORDER BY BRANCH_NO");

            while (rset.next()){
                branchMenuItem[i] = rset.getString("BRANCH_NAME");
                i++;
            }

            i = 0;

            for (i = 0; i < countBranchTableRow; i++){
                branchDropDownMenu.addItem(branchMenuItem[i]);
            }

        }
        catch (Exception e){
            System.out.println("Error connecting database");
        }
    }

    public void enableSaveAndUndoButton(){
        SAVE.setEnabled(true);
        UNDO.setEnabled(true);
    }

    public void disableSaveAndUndoButton(){
        SAVE.setEnabled(false);
        UNDO.setEnabled(false);
    }

    public void confirmationMessage(){
        Object[] options = {"Yes", "Cancel"};

        int n = JOptionPane.showOptionDialog(this,
                "Saving this data will permanently change your data.Do you want to continue?",
                "Confirmation",//title
                JOptionPane.YES_NO_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,        //custom icon
                options,     //the titles of buttons
                options[0]); //default button

        if (n == 0){
            save();
        }
    }

    public int promptSaveBeforeExit(){
        Object[] options = {"Yes", "Discard", "Cancel"};

        int n = JOptionPane.showOptionDialog(this,
                "Some information have changed. If you continue you will lose all unsaved changes.",
                "Confirmation",//title
                JOptionPane.YES_NO_CANCEL_OPTION,
                JOptionPane.WARNING_MESSAGE,
                null,        //custom icon
                options,     //the titles of buttons
                options[0]); //default button

        return n;
    }

    public void confirmationDelete(){
        Object[] options = {"Yes", "Cancel"};

        int n = JOptionPane.showOptionDialog(this,
                "Data deleted will be permanently gone.Do you want to continue?",
                "Confirmation",//title
                JOptionPane.YES_NO_OPTION,
                JOptionPane.WARNING_MESSAGE,
                null,        //custom icon
                options,     //the titles of buttons
                options[0]); //default button

        if (n == 0){
            delete();
        }
    }
    
    public void errorMessage(){

        JOptionPane.showMessageDialog(this,
                "An error has occured. Data cannot be deleted.",
                "Warning",//title
                JOptionPane.WARNING_MESSAGE,
                null);
    }

    public void save(){
        int n = selectedButton();
        String updateString = "";
        
        if (n == 0){
            if (selectMode == 0){
                updateString = "INSERT INTO SALES VALUES (SALES_NO.NEXTVAL," + (SalesTotal.getText())
                         + ",'" + DAYBox.getSelectedItem() + "-"  + MONTHBox.getSelectedItem() + "-"+ YEARField.getText() + "'," + (code) + ")";
                    
                System.out.println(updateString);
            }
          else if (selectMode == 1){
                updateString = "UPDATE SALES SET SALES_TOTAL = " + SalesTotal.getText()
                        + ", SALES_DATE = '" + DAYBox.getSelectedItem() + "-" + MONTHBox.getSelectedItem() + "-" + YEARField.getText() + "', BRANCH_NO = " + code
                        + " WHERE SALES_NO = " + (newTableSales.getValueAt(Table.getSelectedRow(), 0)) + "";
            }

        }
        else if (n == 1){
            if (selectMode == 0){
                updateString = "INSERT INTO INVENTORY VALUES (INV_NO.NEXTVAL, '" + (InvName.getText())
                         + "'," + (InvQty.getText()) + ","+ (InvCost.getText())+","+ code + ")";

                System.out.println(updateString);
            }
          else if (selectMode == 1){
                updateString = "UPDATE INVENTORY SET INV_NAME = '" + InvName.getText()
                        + "',INV_QTY = " + InvQty.getText() + ",INV_COST = " + InvCost.getText() + ", VENDOR_NO = " + code
                        + " WHERE INV_NO = " + (newTableInventory.getValueAt(Table.getSelectedRow(), 0)) + "";
            }
        }
         else if (n == 2){
            if (selectMode == 0){
                updateString = "INSERT INTO ASSET VALUES (ASSET_NO.NEXTVAL, '" + (assetNameField.getText())
                         + "'," + (assetQTYField.getText()) + ","+ (assetCostField.getText())+")";

                System.out.println(updateString);
            }
          else if (selectMode == 1){
                updateString = "UPDATE ASSET SET ASSET_NAME = '" + assetNameField.getText()
                        + "',ASSET_QTY = " + assetQTYField.getText() + ",ASSET_COST = " + assetCostField.getText()
                        + " WHERE ASSET_NO = " + (newTableAsset.getValueAt(Table.getSelectedRow(), 0));
            }
        }
        
        System.out.println(updateString);

        try{
            Statement stmt = con.createStatement();
            stmt.executeUpdate(updateString);
        }
        catch (Exception e){
            System.out.print("Error connecting to Database\n");
        }
        
        getTableInfo();
        setTable();
        clearFields();
        disableSaveAndUndoButton();
        Table.repaint();
    }

    public void delete(){

        
        int n = selectedButton();
        String deleteString = "";
        
        if (Table.getSelectedRow() != -1 ){
            if (n == 0){
                deleteString =  "DELETE FROM SALES WHERE SALES_NO = '" + newTableSales.getValueAt(Table.getSelectedRow(), 0) + "'";
            }
            else if (n == 1){
                deleteString =  "DELETE FROM INVENTORY WHERE INV_NO = '" + newTableInventory.getValueAt(Table.getSelectedRow(), 0) + "'";
            }
            
        }

        System.out.println(deleteString);

        try{
            Statement stmt = con.createStatement();
            stmt.executeUpdate(deleteString);
        }
        catch (Exception e){
            errorMessage();
        }

        getTableInfo();
        setTable();
        clearFields();
        disableSaveAndUndoButton();
        Table.repaint();
    }
}
